package 富婆梦.第一阶段.第八周Scoket.聊天程序;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * @ProjectName: java-learning
 * @Package: 富婆梦.第一阶段.第八周Scoket
 * @ClassName: SimpleCjatClientA
 * @Author: hemengjin
 * @Description: 聊天程序客户端
 * @Date:2021/9/20 15:36
 * @Version: 1.0
 */
public class SimpleChatClientB {
    JTextField outgoing;
    JTextArea incoming;
    PrintWriter writer;
    BufferedReader reader;

    Socket sc;

    {
        try {
            sc = new Socket("localhost", 8888);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void go() {
        //注册按钮的监听者
        //调用setUpNetWorking()
        JFrame frame = new JFrame("Simple Chat Client");
        JPanel mainPanel = new JPanel();

        incoming = new JTextArea(15, 50);
        incoming.setLineWrap(true);
        incoming.setWrapStyleWord(true);
        incoming.setEditable(false);
        JScrollPane qScroll = new JScrollPane(incoming);
        qScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        qScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);


        outgoing = new JTextField(20);
        JButton sendButton = new JButton("Send");
        sendButton.addActionListener(new SendButtonListener());
        mainPanel.add(qScroll);
        mainPanel.add(outgoing);
        mainPanel.add(sendButton);
        setUpNetworking();

        Thread readerThread = new Thread(new IncomingReader());
        readerThread.start();

        frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
        frame.setSize(400, 500);
        frame.setVisible(true);
        //getUpNetworking();
    }

    //建立Scoket链接
    //赋值PrintWriter给实例变量
    private void setUpNetworking() {
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(sc.getInputStream());
            reader = new BufferedReader(inputStreamReader);
            writer = new PrintWriter(sc.getOutputStream());
            System.out.println("networking established");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public class IncomingReader implements Runnable {
        public void run() {
            String meaaage;
            try {
                while ((meaaage = reader.readLine()) != null) {
                    System.out.println("接收到服务端消息：" + meaaage);
                    incoming.append(meaaage + "\n");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public class SendButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            //取得文字字段内容
            //传送到服务器上
            try {
                //开始写数据，通过println将数据传到服务器
                writer.println(outgoing.getText());
                //flush()方法用于刷新此Writer流。 并且一个flush()调用将刷新所有缓冲区中的数据。一系列Writer和输出流。
                //当流将来自不同write()方法的任何字符保存到缓冲区中后，立即将它们写入其预期的目标，然后在检查该目标是否包含另一个字符或字节流后，将其刷新
                writer.flush();
                //System.out.println(outgoing.getText());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            outgoing.setText("");
            //获得焦点
            outgoing.requestFocus();
        }
    }

    public static void main(String[] args) {
        new SimpleChatClientB().go();
    }

}


